//
//  NYMainManager.m
//  Nooryes
//
//  Created by baken on 2020/2/29.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYMainManager.h"
#import "NYMainModel.h"
#import "NYVersionModel.h"


@implementation NYMainManager

//
+(void)checkVersionWithCompletionHandle:(void (^)(NYVersionModel *model, NSError *))complete{
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kCheckVersion]
    parameters:nil
completionHandler:^(id responseObj, NSError *error) {
    NYVersionModel *model = [NYVersionModel mj_objectWithKeyValues:responseObj];
    if (model.code != 0) {
        error = [self createError:model.msg];
    }
    complete(model,error);
}];
    
}


+ (void)modifyAvatarWithImage:(UIImage *)image andCompletionHandle:(void(^)(NYMainModel *model,NSError *error))complete{
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[self convertDataToHexStr:imageData] forKey:@"image"];
    [dict setValue:@".jpeg" forKey:@"type"];
    [dict setValue:sessionid forKey:@"sessionid"];
    [dict setValue:uid forKey:@"userid"];
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kModifyAvatar]
    parameters:dict
completionHandler:^(id responseObj, NSError *error) {
        NYMainModel *model = [NYMainModel mj_objectWithKeyValues:responseObj];
        if (model.code != 0) {
            error = [self createError:model.msg];
        }
        complete(model,error);
    }];
    
    
    //      NSString *param = [NSString stringWithFormat:@"userid=%@&image=%@&sessionid=%@&type=.jpeg",[[NSUserDefaults standardUserDefaults] objectForKey:@"userid"],[self convertDataToHexStr:imageData],[[NSUserDefaults standardUserDefaults] objectForKey:@"sessionid"]];
    
    //    [self POSTT:[NSString stringWithFormat:@"%@%@",Api_URL,kModifyAvatar]
    //    parameters:param
    //completionHandler:^(id responseObj, NSError *error) {
    //        NYMainModel *model = [NYMainModel mj_objectWithKeyValues:responseObj];
    //        if (model.code != 0) {
    //            error = [self createError:model.msg];
    //        }
    //        complete(model,error);
    //    }];
}

//+ (void)upLoadPostImageWithImage:(UIImage *)image andCompletionHandle:(void (^)(NSString *, NSError *))complete{
//    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
//    NSString *imageStr = [self convertDataToHexStr:imageData];
//    NSString *param = [NSString stringWithFormat:@"userid=%@&image=%@&sessionid=%@&imagetype=.png",[[NSUserDefaults standardUserDefaults] objectForKey:@"userid"],imageStr,[[NSUserDefaults standardUserDefaults] objectForKey:@"sessionid"]];
//    
//    [BaseNetManager POSTBase64:[NSString stringWithFormat:@"%@%@",Api_URL,kUploadImg]
//                     parameters:param
//              completionHandler:^(id responseObj, NSError *error) {
//        NSString *imageUrl ;
//        if (!error) {
//          
//        }
//        complete(imageUrl,error);
//    }];
//}


+ (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}
@end
